class Solution(object):
    def topKFrequent(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: List[int]
        """
        numDict = {}
        for num in nums:
            if num not in numDict:
                numDict.update({num:0})
            numDict[num]+=1
        timeDict = {}
        for num, time in numDict.items():
            if time not in timeDict:
                timeDict.update({time:set()})
            timeDict[time].add(num)
        timeList = list(timeDict.keys())
        timeList.sort(reverse=True)
        results = []
        for time in timeList:
            if len(results)<k:
                results.extend(timeDict[time])
            else:
                break
        return results

print(Solution().topKFrequent([1,1,1,2,2,3], 2))
